<?php
namespace Tbf\Bundle\ImageBundle;
use Tbf\Bundle\TbfBundle\Exception\BusinessException;
use Tbf\Bundle\UploadBundle\BlobInterface;
use Tbf\Bundle\UploadBundle\MemoryBlob;
use Tbf\Bundle\UploadBundle\TransformerInterface;

/**
 * 打水印
 */
class WaterMarkTransformer implements TransformerInterface
{
    /** @var  \Tbf\Bundle\ImageBundle\Service\WaterMark */
    protected $config_obj;
    protected $quality;
    public function __construct($config_obj,$quality)
    {
        $this->config_obj = $config_obj;
        $this->quality = $quality;
    }
    public function getConfig()
    {
        //下面的位置和宽度,高度均是以需要打水印的图片的尺寸的比例
        return array(
            //起始点的位置
            'buttom'=>$this->config_obj->getStartPointBottom(),
            'right'=>$this->config_obj->getStartPointRight(),
            'width'=>$this->config_obj->getWaterMarkWidth(),
            'mark_file'=>$this->config_obj->getMarkFilePath(),
        );
    }

    /**
     * @param  BlobInterface $input
     * @return BlobInterface $output
     * @return error
     */
    public function transform(BlobInterface $input)
    {
        list($content,$err) = $input->getContent();
        if ($err!==null) {
            return array(null,$err);
        }
        $input_image = \WideImage::loadFromString($content);
        $config = $this->getConfig();
        $mark_image = \WideImage::loadFromFile($config['mark_file']);
        $input_size = array(
            'input_width'=>$input_image->getWidth(),
            'input_height'=>$input_image->getHeight(),
            'mark_width'=>$mark_image->getWidth(),
            'mark_height'=>$mark_image->getHeight(),
        );

        $size = $this->getWaterMarkSize($input_size);

        $mark_image = $mark_image->resize(
            $this->sizeToInt($size['mark_width']),
            $this->sizeToInt($size['mark_height']),'fill');
        $output_image = $input_image->merge($mark_image,
            $this->posToInt($size['left']),
            $this->posToInt($size['top']));
        $after_content = $output_image->asString('jpg',$this->quality);

        return array(new MemoryBlob($after_content,'jpg'),null);
    }

    /**
     * 计算位置参数
     * 水印需要保持比例
     * 水印不能超出图片边界
     */
    private function getWaterMarkSize($data)
    {
        $config = $this->getConfig();
        $buttom = $data['input_height'] * $config['buttom'];
        $right = $data['input_width'] * $config['right'];

        //水印需要保持比例
        $mark_ratio = $data['mark_height'] / $data['mark_width'];
        $mark_width = $data['input_width'] * $config['width'];

        //水印不能超出图片边界
        $max_width = $data['input_width'] - $right;
        $max_height = $data['input_height'] - $buttom;

        if ($max_width < $mark_width) {
            $mark_width = $max_width;
        }

        $mark_height = $mark_width * $mark_ratio;
        if ($max_height < $mark_height) {
            $mark_height = $max_height;
            $mark_width = $mark_height / $mark_ratio;
        }

        $left = $data['input_width'] - $right - $mark_width;
        $top = $data['input_height'] - $buttom - $mark_height;

        return compact(array('left','top','mark_width','mark_height'));
    }
    private function sizeToInt($size)
    {
        if ($size<=0) {
            throw new BusinessException('dimensions must be larger than 0:'.$size);
        }
        $size_int = round($size);
        if ($size_int<=0) {
            return 1;
        }

        return $size_int;
    }
    private function posToInt($pos)
    {
        $pos_int = round($pos);

        return $pos_int;
    }
}
